﻿using IBLL;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Model;
using NPOI.XSSF.UserModel;

namespace ZY19.API.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class UserController : ControllerBase
    {
        public UserController(IUserBLL bll)
        {
            Bll = bll;
        }

        public IUserBLL Bll { get; }
        [HttpPost]
        public Task<int> AddUser(UserModel user)
        {
            return Bll.AddUser(user);
        }
        [HttpGet]
        public dynamic UserShow(string? name, int tid)
        {
            return Bll.UserShow(name, tid);
        }
        [HttpGet]
        public List<UserModel> Users()
        {
            return Bll.Users();
        }
        [HttpGet]
        public IActionResult Dc() 
        {
            List<UserModel>user =Bll.Users();
            XSSFWorkbook sheets= new XSSFWorkbook();
            var sheet= sheets.CreateSheet("职员信息");
            var row=sheet.CreateRow(0);
            row.CreateCell(0).SetCellValue("Id");
            row.CreateCell(1).SetCellValue("UserName");
            row.CreateCell(2).SetCellValue("Sex");
            row.CreateCell(3).SetCellValue("tid");
            row.CreateCell(4).SetCellValue("IDCard");
            row.CreateCell(5).SetCellValue("Phone");
            row.CreateCell(6).SetCellValue("IsDelete");
            row.CreateCell(7).SetCellValue("CreteId");
            row.CreateCell(8).SetCellValue("CreateTime");
            row.CreateCell(9).SetCellValue("UpdateId");
            row.CreateCell(10).SetCellValue("UPdateTime");
            row.CreateCell(11).SetCellValue("DeleteId");
            row.CreateCell(0).SetCellValue("DeleteTime");
            int num = 1;
            foreach (var item in user)
            {
                var r = sheet.CreateRow(num);
                r.CreateCell(0).SetCellValue(item.Id);
                r.CreateCell(1).SetCellValue(item.UserName);
                r.CreateCell(2).SetCellValue(item.Sex==true?"男":"女");
                r.CreateCell(3).SetCellValue(item.tid==1?"财务总监":item.tid==2?"项目经理":"普通职员");
                r.CreateCell(4).SetCellValue(item.IDCard);
                r.CreateCell(5).SetCellValue(item.Phone);
                r.CreateCell(6).SetCellValue(item.Phone);
                r.CreateCell(7).SetCellValue(item.IsDelete);
                r.CreateCell(8).SetCellValue(item.CreateTime==null?"":item.CreateTime.ToString("yyyy-MM-DD HH:mm:dd"));
                r.CreateCell(9).SetCellValue(item.UpdateId);
                r.CreateCell(10).SetCellValue(item.UpdateTime == null ? "" : item.UpdateTime.ToString("yyyy-MM-DD HH:mm:dd"));
                r.CreateCell(11).SetCellValue(item.DeleteId);
                r.CreateCell(0).SetCellValue(item.DeleteTime == null ? "" : item.DeleteTime.ToString("yyyy-MM-DD HH:mm:dd"));
                num++;
            }
            MemoryStream stream= new MemoryStream();
            sheets.Write(stream);
            byte[]data=stream.ToArray();




            string mine = new Microsoft.AspNetCore.StaticFiles.FileExtensionContentTypeProvider().Mappings[".xlsx"];
            return File(data, mine, "职员.xlsx");

        }
    }
}
